package com.future.mayi.fragment;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.adapter.AppraisalAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.CateModel;
import com.future.mayi.response.CateListResponse;

import butterknife.BindView;

public class AppraisalFragment extends BaseFragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private AppraisalAdapter adapter;

    private CateModel cateModel;
    private AppraisalAdapter.AppraisalAdapterListener listener;


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_appraisal_item;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new AppraisalAdapter(getActivity());
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        adapter.setListener(listener);
    }

    @Override
    protected void initData() {
        setCateModel(cateModel, listener);
    }

    private void getData() {
        new HttpRequest<CateListResponse>()
                .with(getActivity())
                .addParam("pid", String.valueOf(cateModel.getCate_id()))
                .addParam("token", App.getToken(mContext))
                .setApiCode(ApiCst.getBrandOrCate)
                .setListener(new HttpRequest.OnNetworkListener<CateListResponse>() {
                    @Override
                    public void onSuccess(CateListResponse response) {
                        cateModel.setList(response.getCateList());
                        adapter.setList(response.getCateList());
                    }

                    @Override
                    public void onFail(String message, CateListResponse response) {
                        toast(message);
                    }
                })
                .start(new CateListResponse());
    }

    public void setCateModel(CateModel cateModel, AppraisalAdapter.AppraisalAdapterListener listener) {
        this.cateModel = cateModel;
        this.listener = listener;
        if (cateModel != null && adapter != null)
            getData();
    }

    public CateModel getCateModel() {
        return cateModel;
    }
}
